﻿using System;
using System.Windows;
using System.Windows.Media;
using WickedFlame.Core.State;

namespace WickedFlame.Core.Shapes
{
    public class PolygonShape : VectorShape, IPolygonShape, IStateCapturable, ICopyable
    {
        public PolygonShape()
            : this(new Point(0, 0))
        {
        }

        public PolygonShape(Point start)
        {
            _definingGeometry = new PathGeometry();

            var col = new PathSegmentCollection();
            var fig = new PathFigure(start, col, false);
            _definingGeometry.Figures.Add(fig);
        }

        public PolygonShape(string source)
        {
            _definingGeometry = (PathGeometry)Geometry.Parse(source);
        }

        PathGeometry _definingGeometry;
        public PathGeometry DefiningGeometry
        {
            get
            {
                if (_definingGeometry == null)
                {
                    _definingGeometry = new PathGeometry();

                    var col = new PathSegmentCollection();
                    var fig = new PathFigure(StartPoint, col, false);
                    _definingGeometry.Figures.Add(fig);
                }

                return _definingGeometry;
            }
        }

        public string GeometryString
        {
            get
            {
                return DefiningGeometry.ToString();
            }
        }

        public override Rect Bounds
        {
            get
            {
                return DefiningGeometry.Bounds;
            }
        }

        //protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        //{
        //    base.OnPropertyChanged(e);

        //    DependencyProperty prop = e.Property;
        //    if (prop == )
        //    {
        //    //JoinEndSegments
        //}

        public override void Draw(DrawingContext drawingContext)
        {
            drawingContext.DrawGeometry(
                Fill,
                //new Pen(Stroke, StrokeThickness),
                Pen,
                DefiningGeometry);
        }

        public override void DrawTracker(DrawingContext drawingContext, double scale)
        {
            var p = new Pen(Brushes.Blue, 0.5 / scale);
            var p2 = new Pen(Brushes.Blue, 1 / scale);


            Point lastSegmentPoint = _definingGeometry.Figures[0].StartPoint;
            foreach (PathSegment segment in _definingGeometry.Figures[0].Segments)
            {
                if (segment is LineSegment)
                {
                    drawingContext.DrawLine(p, lastSegmentPoint, ((LineSegment)segment).Point);

                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((LineSegment)segment).Point, 2.5 / scale, 2.5 / scale);

                    lastSegmentPoint = ((LineSegment)segment).Point;
                }
                else if (segment is QuadraticBezierSegment)
                {
                    drawingContext.DrawLine(p, lastSegmentPoint, ((QuadraticBezierSegment)segment).Point1);
                    drawingContext.DrawLine(p, ((QuadraticBezierSegment)segment).Point1, ((QuadraticBezierSegment)segment).Point2);

                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((QuadraticBezierSegment)segment).Point1, 2.5 / scale, 2.5 / scale);
                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((QuadraticBezierSegment)segment).Point2, 2.5 / scale, 2.5 / scale);

                    lastSegmentPoint = ((QuadraticBezierSegment)segment).Point2;
                }
                else if (segment is BezierSegment)
                {
                    //Pen p = new Pen(Brushes.Blue, 0.5);
                    drawingContext.DrawLine(p, lastSegmentPoint, ((BezierSegment)segment).Point1);
                    drawingContext.DrawLine(p, ((BezierSegment)segment).Point2, ((BezierSegment)segment).Point3);

                    //p = new Pen(Brushes.Blue, 1);
                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((BezierSegment)segment).Point1, 2.5 / scale, 2.5 / scale);
                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((BezierSegment)segment).Point2, 2.5 / scale, 2.5 / scale);
                    drawingContext.DrawEllipse(Brushes.LightBlue, p2, ((BezierSegment)segment).Point3, 2.5 / scale, 2.5 / scale);

                    lastSegmentPoint = ((BezierSegment)segment).Point3;
                }
                else
                    throw new NotImplementedException("Segmenttype is not jet implemented");
            }

            if (!(SelectedTracker == 1))
                drawingContext.DrawEllipse(Brushes.LightBlue, p2, _definingGeometry.Figures[0].StartPoint, 2.5 / scale, 2.5 / scale);
            else
                drawingContext.DrawEllipse(Brushes.White, new Pen(Brushes.Black, 2 / scale), _definingGeometry.Figures[0].StartPoint, 2.5 / scale, 2.5 / scale);

            if(SelectedTracker==TrackerCount)
                drawingContext.DrawEllipse(Brushes.White, new Pen(Brushes.Black, 2 / scale), GetTracker(SelectedTracker), 2.5 / scale, 2.5 / scale);

        }

        public Point StartPoint
        {
            get
            {
                return DefiningGeometry.Figures[0].StartPoint;
            }
            set
            {
                DefiningGeometry.Figures[0].StartPoint = value;
            }
        }

        public Point EndPoint
        {
            get
            {
                var point = new Point(0, 0);
                if (DefiningGeometry.Figures[0].Segments.Count > 0)
                {
                    PathSegment segment = DefiningGeometry.Figures[0].Segments[DefiningGeometry.Figures[0].Segments.Count - 1];

                    if (segment is LineSegment)
                    {
                        point = ((LineSegment)segment).Point;
                    }
                    else if (segment is QuadraticBezierSegment)
                    {
                        point = ((QuadraticBezierSegment)segment).Point2;
                    }
                    else if (segment is BezierSegment)
                    {
                        point = ((BezierSegment)segment).Point3;
                    }
                    else
                        throw new NotImplementedException("Segmenttype is not jet implemented");
                }
                return point;
            }
        }

        private PathSegmentCollection Segments
        {
            get
            {
                return DefiningGeometry.Figures[0].Segments;
            }
            set
            {
                DefiningGeometry.Figures[0].Segments = value;
            }
        }

        //public override VectorShape Clone()
        //{
        //    var shape = new VECPolygon(this.StartPoint);
        //    //shape.StartPoint = this.StartPoint;

        //    foreach (PathSegment segment in Segments)
        //    {
        //        if (segment is LineSegment)
        //        {
        //            shape.Segments.Add(new LineSegment(((LineSegment)segment).Point, true));
        //        }
        //        else if (segment is QuadraticBezierSegment)
        //        {
        //            shape.Segments.Add(new QuadraticBezierSegment(((QuadraticBezierSegment)segment).Point1,
        //                ((QuadraticBezierSegment)segment).Point2, true));
        //        }
        //        else
        //            throw new NotImplementedException("Segmenttype is not jet implemented");
        //    }

        //    CopyShapeProperies(shape);
        //    return shape;
        //}

        public override bool IntersectsWith(Rect rectangle)
        {
            var rectgeo = new RectangleGeometry(rectangle, 0, 0);
            var det = DefiningGeometry.StrokeContainsWithDetail(Pen/*new Pen(this.Stroke, this.StrokeThickness)*/, rectgeo);

            if (det == IntersectionDetail.FullyContains || det == IntersectionDetail.FullyInside || det == IntersectionDetail.Intersects)
                return true;

            return false;
        }

        public void AddLineSegment(Point point)
        {
            DefiningGeometry.Figures[0].Segments.Add(new LineSegment(point, true));
            this.Invalidate();
        }

        public void AddSegment(PathSegment segment, Point startPoint)
        {
            //TODO!
            //check wich side is selected and add the segment to that side!
            if (SelectedTracker == 1)
            {
                if (segment is LineSegment)
                {
                    (segment as LineSegment).Point = StartPoint;
                    //DefiningGeometry.Figures[0].Segments.Insert(0, segment);
                    //StartPoint = startPoint;
                }
                else if (segment is QuadraticBezierSegment)
                {
                    (segment as QuadraticBezierSegment).Point2 = StartPoint;
                    //StartPoint = startPoint;
                }
                else if (segment is BezierSegment)
                {
                    (segment as BezierSegment).Point3 = StartPoint;
                    //StartPoint = startPoint;
                }
                else
                    throw new ShapeException("Segmenttype is not jet implemented");

                DefiningGeometry.Figures[0].Segments.Insert(0, segment);
                StartPoint = startPoint;
            }
            else
                DefiningGeometry.Figures[0].Segments.Add(segment);

            this.Invalidate();
        }

        public bool JoinEndSegments
        {
            get
            {
                return DefiningGeometry.Figures[0].IsClosed;
            }
            set
            {
                if (value)
                    Segments.RemoveAt(Segments.Count - 1);
                else
                    Segments.Add(new LineSegment(new Point(DefiningGeometry.Figures[0].StartPoint.X, DefiningGeometry.Figures[0].StartPoint.Y), false));
                
                DefiningGeometry.Figures[0].IsClosed = value;
            }
        }

        public override int TrackerCount
        {
            get
            {
                int cnt = 0;
                foreach (PathSegment segment in DefiningGeometry.Figures[0].Segments)
                {
                    if (segment is LineSegment)
                        cnt++;
                    else if (segment is QuadraticBezierSegment)
                        cnt += 2;
                    else if (segment is BezierSegment)
                        cnt += 3;
                    else
                        throw new NotImplementedException("Segmenttype is not jet implemented");
                }

                return cnt + 1; // add start point
            }
        }

        public override void SelectTracker(int tracker)
        {
            if (tracker == 1 || tracker == TrackerCount)
                base.SelectTracker(tracker);
        }

        public int MaxHandle
        {
            get
            {
                return DefiningGeometry.Figures[0].Segments.Count + 1;
            }
        }

        /// <summary>
        /// Get handle point by 1-based number
        /// </summary>
        /// <param name="trackerNo"></param>
        /// <returns></returns>
        public override Point GetTracker(int trackerNo)
        {
            if (trackerNo == 1)
                return DefiningGeometry.Figures[0].StartPoint;

            int ti = 0;
            int i = 0;
            int tcnt = 0;
            while (ti < trackerNo-2)
            {
                if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
                {
                    i++;
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
                {
                    tcnt++;

                    if (tcnt == 2)
                    {
                        i++;
                        tcnt = 0;
                    }
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
                {
                    tcnt++;

                    if (tcnt == 3)
                    {
                        i++;
                        tcnt = 0;
                    }
                }
                else
                    throw new NotImplementedException("Segmenttype is not jet implemented");

                ti++;
            }

            if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
            {
                return ((LineSegment)DefiningGeometry.Figures[0].Segments[i]).Point;
            }
            else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
            {
                if (tcnt == 1)
                {
                    return ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2;
                }
                else
                {
                    return ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1;
                }
            }
            else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
            {
                if (tcnt == 2)
                {
                    return ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point3;
                }
                if (tcnt == 1)
                {
                    return ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2;
                }
                else
                {
                    return ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1;
                }
            }
            else
                throw new NotImplementedException("Segmenttype is not jet implemented");

            //if (trackerNo - 2 >= 0 && trackerNo - 2 <= DefiningGeometry.Figures[0].Segments.Count)
            //{
            //    if (DefiningGeometry.Figures[0].Segments[trackerNo - 2] is LineSegment)
            //        return ((LineSegment)DefiningGeometry.Figures[0].Segments[trackerNo - 2]).Point;
            //    else if(DefiningGeometry.Figures[0].Segments[trackerNo - 2] is QuadraticBezierSegment)
            //        return ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[trackerNo - 2]).Point2;
            //}

            return DefiningGeometry.Figures[0].StartPoint;
        }

        public override System.Windows.Input.Cursor GetTrackerCursor(int trackerNo)
        {
            return System.Windows.Input.Cursors.SizeAll;
        }

        ///// <summary>
        ///// Hit test
        ///// </summary>
        ///// <param name="point"></param>
        ///// <returns></returns>
        //public override int HitTestInt(Point point)
        //{
        //    if (IsSelected)
        //    {
        //        for (int i = 1; i <= TrackerCount; i++)
        //        {
        //            if (GetTrackerRectangle(i).Contains(point))
        //            {
        //                return i;
        //            }
        //        }
        //    }

        //    if (PointInObject(point))
        //        return 0;

        //    return -1;
        //}

        protected override bool PointInObject(Point point)
        {
            return DefiningGeometry.FillContains(point);
        }

        /// <summary>
        /// Move handle to new point (resizing)
        /// </summary>
        /// <param name="point"></param>
        /// <param name="trackerNo"></param>
        public override void MoveTracker(Point point, int trackerNo)
        {
            if (trackerNo == 1)
                DefiningGeometry.Figures[0].StartPoint = point;
            else
            {
                int ti = 0;
                int i = 0;
                int tcnt = 0;
                while (ti < trackerNo - 2)
                {
                    if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
                    {
                        i++;
                    }
                    else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
                    {
                        tcnt++;

                        if (tcnt == 2)
                        {
                            i++;
                            tcnt = 0;
                        }
                    }
                    else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
                    {
                        tcnt++;

                        if (tcnt == 3)
                        {
                            i++;
                            tcnt = 0;
                        }
                    }
                    else
                        throw new NotImplementedException("Segmenttype is not jet implemented");

                    ti++;
                }

                if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
                {
                    ((LineSegment)DefiningGeometry.Figures[0].Segments[i]).Point = point;
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
                {
                    if (tcnt == 1)
                    {
                        ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2 = point;
                    }
                    else
                    {
                        ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1 = point;
                    }
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
                {
                    if (tcnt == 2)
                    {
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point3 = point;
                    }
                    if (tcnt == 1)
                    {
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2 = point;
                    }
                    else
                    {
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1 = point;
                    }
                }
                else
                    throw new NotImplementedException("Segmenttype is not jet implemented");
            }

            this.Invalidate();

            CallChangedHandler();
        }

        public override void Move(double deltaX, double deltaY)
        {
            StartPoint = new Point(StartPoint.X + deltaX, StartPoint.Y + deltaY);

            foreach (PathSegment segment in DefiningGeometry.Figures[0].Segments)
            {
                if (segment is LineSegment)
                {
                    ((LineSegment)segment).Point = new Point(
                        ((LineSegment)segment).Point.X + deltaX,
                        ((LineSegment)segment).Point.Y + deltaY);
                }
                else if (segment is QuadraticBezierSegment)
                {
                    ((QuadraticBezierSegment)segment).Point1 = new Point(
                        ((QuadraticBezierSegment)segment).Point1.X + deltaX,
                        ((QuadraticBezierSegment)segment).Point1.Y + deltaY);

                    ((QuadraticBezierSegment)segment).Point2 = new Point(
                        ((QuadraticBezierSegment)segment).Point2.X + deltaX,
                        ((QuadraticBezierSegment)segment).Point2.Y + deltaY);
                }
                else if (segment is BezierSegment)
                {
                    ((BezierSegment)segment).Point1 = new Point(
                        ((BezierSegment)segment).Point1.X + deltaX,
                        ((BezierSegment)segment).Point1.Y + deltaY);

                    ((BezierSegment)segment).Point2 = new Point(
                        ((BezierSegment)segment).Point2.X + deltaX,
                        ((BezierSegment)segment).Point2.Y + deltaY);

                    ((BezierSegment)segment).Point3 = new Point(
                        ((BezierSegment)segment).Point3.X + deltaX,
                        ((BezierSegment)segment).Point3.Y + deltaY);
                }
                else
                    throw new NotImplementedException("Segmenttype is not jet implemented");
            }

            this.Invalidate();

            CallMovedHandler();
        }

        #region IPoligonShape Member

        public void AddShapeToPoligon(PolygonShape poligon)
        {
            //TODO:
            //Flip the polygon if te first tracker is selected!

            if (SelectedTracker > 0)
            {
                foreach (PathSegment segment in DefiningGeometry.Figures[0].Segments)
                {
                    if (segment is LineSegment)
                    {
                        poligon.AddSegment(new LineSegment(
                            ((LineSegment)segment).Point,
                            true), 
                            new Point());
                    }
                    else if (segment is QuadraticBezierSegment)
                    {
                        poligon.AddSegment(new QuadraticBezierSegment(
                            ((QuadraticBezierSegment)segment).Point1,
                            ((QuadraticBezierSegment)segment).Point2,
                            true),
                            new Point());
                    }
                    else if (segment is BezierSegment)
                    {
                        poligon.AddSegment(new BezierSegment(
                            ((BezierSegment)segment).Point1,
                            ((BezierSegment)segment).Point2,
                            ((BezierSegment)segment).Point3,
                            true),
                            new Point());
                    }
                    else
                        throw new ShapeException("Segmenttype is not jet implemented");
                }
            }
            else
                throw new ShapeException("No Tracker is isSelected");
        }

        public PolygonShape CreatePolygon()
        {
            return this;
        }

        #endregion

        #region IHistoryable Members

        public ShapeMemento CreateMementoState()
        {
            var memento = new PolygonMemento();
            memento.SaveProperties(this);

            //foreach (PathSegment segment in DefiningGeometry.Figures[0].Segments)
            //{
            //    if (segment is LineSegment)
            //    {
            //        ((PolygonMemento)memento).Segments.Add(new LineSegment(
            //            ((LineSegment)segment).Point, 
            //            ((LineSegment)segment).IsStroked));
            //    }
            //    else if (segment is QuadraticBezierSegment)
            //    {
            //        ((PolygonMemento)memento).Segments.Add(new QuadraticBezierSegment(
            //            ((QuadraticBezierSegment)segment).Point1, 
            //            ((QuadraticBezierSegment)segment).Point2, 
            //            ((QuadraticBezierSegment)segment).IsStroked));
            //    }
            //    else if (segment is BezierSegment)
            //    {
            //        ((PolygonMemento)memento).Segments.Add(new BezierSegment(
            //            ((BezierSegment)segment).Point1,
            //            ((BezierSegment)segment).Point2,
            //            ((BezierSegment)segment).Point2,
            //            ((BezierSegment)segment).IsStroked));
            //    }
            //    else
            //        throw new NotImplementedException("Segmenttype is not jet implemented");
            //}

            return memento;
        }

        public void ResetState(ShapeMemento memento)
        {
            memento.ResetProperties(this);

            //DefiningGeometry.Figures[0].Segments.Clear();
            //foreach (PathSegment segment in ((PolygonMemento)memento).Segments)
            //{
            //    if (segment is LineSegment)
            //    {
            //        DefiningGeometry.Figures[0].Segments.Add(new LineSegment(
            //            ((LineSegment)segment).Point, 
            //            ((LineSegment)segment).IsStroked));
            //    }
            //    else if (segment is QuadraticBezierSegment)
            //    {
            //        DefiningGeometry.Figures[0].Segments.Add(new QuadraticBezierSegment(
            //            ((QuadraticBezierSegment)segment).Point1, 
            //            ((QuadraticBezierSegment)segment).Point2, 
            //            ((QuadraticBezierSegment)segment).IsStroked));
            //    }
            //    else if (segment is BezierSegment)
            //    {
            //        DefiningGeometry.Figures[0].Segments.Add(new BezierSegment(
            //            ((BezierSegment)segment).Point1,
            //            ((BezierSegment)segment).Point2,
            //            ((BezierSegment)segment).Point2,
            //            ((BezierSegment)segment).IsStroked));
            //    }
            //    else
            //        throw new NotImplementedException("Segmenttype is not jet implemented");
            //}
        }

        #endregion

        public override void MoveTracker(int tracker, double deltaX, double deltaY)
        {
            //throw new NotImplementedException();

            if (tracker == 1)
                DefiningGeometry.Figures[0].StartPoint = new Point(DefiningGeometry.Figures[0].StartPoint.X + deltaX, DefiningGeometry.Figures[0].StartPoint.Y + deltaY);
            else
            {
                int ti = 0;
                int i = 0;
                int tcnt = 0;
                while (ti < tracker - 2)
                {
                    if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
                    {
                        i++;
                    }
                    else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
                    {
                        tcnt++;

                        if (tcnt == 2)
                        {
                            i++;
                            tcnt = 0;
                        }
                    }
                    else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
                    {
                        tcnt++;

                        if (tcnt == 3)
                        {
                            i++;
                            tcnt = 0;
                        }
                    }
                    else
                        throw new NotImplementedException("Segmenttype is not jet implemented");

                    ti++;
                }

                if (DefiningGeometry.Figures[0].Segments[i] is LineSegment)
                {
                    Point point = ((LineSegment)DefiningGeometry.Figures[0].Segments[i]).Point;

                    ((LineSegment)DefiningGeometry.Figures[0].Segments[i]).Point = new Point(point.X + deltaX, point.Y + deltaY);
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is QuadraticBezierSegment)
                {
                    if (tcnt == 1)
                    {
                        Point point = ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2;
                        ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2 = new Point(point.X + deltaX, point.Y + deltaY);
                    }
                    else
                    {
                        Point point = ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1;
                        ((QuadraticBezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1 = new Point(point.X + deltaX, point.Y + deltaY);
                    }
                }
                else if (DefiningGeometry.Figures[0].Segments[i] is BezierSegment)
                {
                    if (tcnt == 2)
                    {
                        Point point = ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point3;
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point3 = new Point(point.X + deltaX, point.Y + deltaY);
                    }
                    if (tcnt == 1)
                    {
                        Point point = ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2;
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point2 = new Point(point.X + deltaX, point.Y + deltaY);
                    }
                    else
                    {
                        Point point = ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1;
                        ((BezierSegment)DefiningGeometry.Figures[0].Segments[i]).Point1 = new Point(point.X + deltaX, point.Y + deltaY);
                    }
                }
                else
                    throw new NotImplementedException("Segmenttype is not jet implemented");
            }

            this.Invalidate();

            CallChangedHandler();
        }

        #region ICopyable Members

        public VectorShape Copy()
        {
            var shape = new PolygonShape(StartPoint);
            foreach (PathSegment segment in DefiningGeometry.Figures[0].Segments)
            {
                if (segment is LineSegment)
                {
                    shape.AddSegment(new LineSegment(
                        ((LineSegment)segment).Point,
                        true),
                        new Point());
                }
                else if (segment is QuadraticBezierSegment)
                {
                    shape.AddSegment(new QuadraticBezierSegment(
                        ((QuadraticBezierSegment)segment).Point1,
                        ((QuadraticBezierSegment)segment).Point2,
                        true),
                        new Point());
                }
                else if (segment is BezierSegment)
                {
                    shape.AddSegment(new BezierSegment(
                        ((BezierSegment)segment).Point1,
                        ((BezierSegment)segment).Point2,
                        ((BezierSegment)segment).Point3,
                        true),
                        new Point());
                }
                else
                    throw new ShapeException("Segmenttype is not jet implemented");
            }

            shape.Fill = this.Fill;
            shape.Stroke = this.Stroke;
            shape.StrokeThickness = this.StrokeThickness;

            return shape;
        }

        #endregion
    }
}
